/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RMS;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 * @author SamSung
 */
public class ScoreRMS {

    private RecordStore rs = null;
    private RecordEnumeration re = null;
    private String[] names = new String[5];
    private int[] scores = new int[5];

    public ScoreRMS() {
        try {
            rs = RecordStore.openRecordStore("score", true);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
    }

    public void createRecord(String name, int score) {
        System.out.println("score=" + score);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream data = new DataOutputStream(baos);
        try {
            data.writeUTF(name);
            data.writeInt(score);
            data.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        byte[] rec = baos.toByteArray();

        try {
            rs.addRecord(rec, 0, rec.length);
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
        }

        try {
            data.close();
            baos.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public void getNameNscore() {
        try {
            re = rs.enumerateRecords(null, new ScoreCompare(), false);
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        }

        int i = 0;
        while (re.hasNextElement() && i < 5) {
            try {
                byte[] dt = re.nextRecord();
                DataInputStream dis = new DataInputStream(new ByteArrayInputStream(dt));
                try {
                    names[i] = dis.readUTF();
                    scores[i] = dis.readInt();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            } catch (InvalidRecordIDException ex) {
                ex.printStackTrace();
            } catch (RecordStoreNotOpenException ex) {
                ex.printStackTrace();
            } catch (RecordStoreException ex) {
                ex.printStackTrace();
            }
            i++;
        }
    }

    public String[] getNames() {
        return names;
    }

    public int[] getScores() {
        return scores;
    }
}

class ScoreCompare implements RecordComparator {

    public int compare(byte[] score1, byte[] score2) {
        int i1 = 0, i2 = 0;
        DataInputStream ds1 = new DataInputStream(new ByteArrayInputStream(score1));
        DataInputStream ds2 = new DataInputStream(new ByteArrayInputStream(score2));
        try {
            ds1.readUTF();
            ds2.readUTF();
            i1 = ds1.readInt();
            i2 = ds2.readInt();
        } catch (IOException ex) {
            ex.printStackTrace();
        }


        if (i1 > i2) {
            return RecordComparator.PRECEDES;
        }
        if (i1 < i2) {
            return RecordComparator.FOLLOWS;
        }
        return RecordComparator.EQUIVALENT;
    }
}
